import axios from 'axios'
// 配置请求的根域名
axios.defaults.baseURL = 'http://www.520gongfu.com:9527/api/';
//使用axios拦截器,设置全局token
axios.interceptors.request.use(
    config => {
        let token = localStorage.getItem("zxgToken");
        if (token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers.Authorization = `${token}`;
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);

//引入工具
import qs from 'qs'

// 轮播图
export const getsliders = () => {
    return axios.get('/home/sliders').then(res => {
        const { data } = res
        if (data.status == 0) {
            // 返回数据
            return data.data
        } else {
            // 轻提示错误消息
            this.$toast.fail(data.msg)
        }
    })
}

// 分类入口
export const getCategories = () => {
    return axios.get('/home/category').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

// 楼层数据
export const getFloors = () => {
    return axios.get('/home/floorlist').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

// 商品列表
export const getProcuctList = (params) => {
    return axios.get('/product/list', {
        params
    }).then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

// 商品详情列表
export const getProcuctDetail = (params) => {
    return axios.get('/product/detail', {
        params
    }).then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

// 分类数据
export const getcategoryData = () => {
    return axios.get('/category/data').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

// 登录
export const login = (params) => {
    return axios.post('user/login', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

// 注册
export const register = (params) => {
    return axios.post('user/register', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

//获取用户信息
export const getUserInfo = () => {
    // 获取用户token
    // let token = localStorage.getItem('zxgToken')
    return axios.get('/user/info').then(res => {
        const { data } = res
        return data
    })
}

//推荐商品
export const getRecommend = () => {
    return axios.get('/product/recommend').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

//退出登录
export const logout = () => {
    return axios.post('/user/logout').then(res => {
        const { data } = res
        return data
    })
}

//获取购物车数据
export const getCarList = () => {
    return axios.get('/cart/list').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

//加入购物车
export const submitToCar = (params) => {
    return axios.post('/cart/add', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

//删除购物车数据
export const delCar = (params) => {
    return axios.post('/cart/del', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

// 获取QQ 授权的信息
export const getUserQQinfo = (params, callback) => {
    return axios.get('/user/qqinfo', {
        // get请求参数配置
        params: params, // 参数
        // 序列化参数（格式化处理参数）
        paramsSerializer: function(params) {
            return qs.stringify(params)
        }
    }).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}



//  获取 QQ 授权信息之后 
//  获取 当前 应用的登录授权 token
export const getUserQQlogin = (params, callback) => {
    return axios.post('/user/qqlogin', qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}